import ctime from '@/components/changeTime.vue'
import formalitles from '@/components/carDetail/formalities.vue'
import carDetail from '@/components/carDetail/carDetail.vue'
export default {
  data() {
    return {
      id: '',
      storeId: '',
      type: '1', // 1 为待报价页面  2 为我要报价页面
      quotationDateStatus: false, //报价有效期状态
      quotationDate: '', //报价有效期
      dataObj: {
        findBoffList: []
      },
      offerCountList: ['待报价', '已报价', '已过期', '已取消'],
      carAdress: {
        address: '', //车源所在地
        id: 0, //id
      },
      myquotation: null, //报价
      myDeposit: null, //定金
      salesArea: {
        address: '', //车源所在地
        id: '', //id
      },
      formalitiesStatus: false, //手续状态
      formalitiesValue: {}, //手续
      carDetailStatus: false, //详情状态
      carDetail: {}, //详情
      productionDateStatus: false, //生产日期有效期
      productionDate: '', //生产日期
      addRemark: '',
      success: false, //成功弹窗
      saleMsg: {}, //报价方联系人信息
      goIn: null, //上一个页面 1不需要加载报价信息 2回显报价信息
      keystatus: false,
      keys: 0
    }
  },
  components: {
    ctime,
    formalitles,
    carDetail
  },
  onLoad(options) {

    this.id = options.id
    this.storeId = options.storeId
    this.type = options.type
    this.goIn = options.goIn || 1
    console.log(this.type)

    this.GetMyCarFindBDetails()
    this.success = false
    uni.$on('selectCity', res => {
      //1 上牌 2区域需求
      if (res.type === '3') {
        this.carAdress.id = res.value.id
        this.carAdress.address = res.value.name

      } else if (res.type === '4') {
        this.salesArea.id = res.value.id?res.value.id:0
        this.salesArea.address = res.value.name
      }
    })

    uni.$on('selContact', data => {
      console.log(data)
      this.saleMsg = data
    })
  },
  methods: {
    // 获取详情
    async GetMyCarFindBDetails() {
      try {
        let data = {
          'id': this.id,
          'store_Id': this.storeId
        }
        const res = await this.$api.findCar.GetMyCarFindBDetails(data)
        console.log(res)
        this.saleMsg = {
          id: res.data.loginListId,
          name: res.data.loginTruename,
          phone: res.data.loginMobile,
          index: null,
        }
        res.data.facade_pictureList.forEach(item => {
          if (item.inColour) {
            item.inColour.neiColor = item.inColour.colorCoding ? item.inColour.colorCoding.split('/') : []
          }
          if (item.outerColour) {
            item.outerColour.neiColor = item.outerColour.colorCoding ? item.outerColour.colorCoding.split('/') : []
          }
        })
        res.data.addTime = res.data.addTime.split('T')[0] + ' ' + res.data.addTime.split('T')[1]
        this.dataObj = res.data
        if (this.goIn === '2') {
          this.saleMsg.id = res.data.carFindBOneselfDetailsBoffPublicSon.car_quoted_contact_b_id
          this.myquotation = res.data.carFindBOneselfDetailsBoffPublicSon.quoted_Price
          this.myDeposit = res.data.carFindBOneselfDetailsBoffPublicSon.deposit
          this.quotationDate = res.data.carFindBOneselfDetailsBoffPublicSon.deadline.split('T')[0]
          this.carAdress.id = res.data.carFindBOneselfDetailsBoffPublicSon.car_source_region_id
          this.carAdress.address = res.data.carFindBOneselfDetailsBoffPublicSon.car_source_region_name
          this.formalitiesValue.value = res.data.carFindBOneselfDetailsBoffPublicSon.service_Charge
          this.formalitiesValue.label = res.data.carFindBOneselfDetailsBoffPublicSon.service_Chargename
          this.carDetail.value = res.data.carFindBOneselfDetailsBoffPublicSon.car_Details
          this.carDetail.label = res.data.carFindBOneselfDetailsBoffPublicSon.car_Detailsname
          this.salesArea.id = res.data.carFindBOneselfDetailsBoffPublicSon ? res.data.carFindBOneselfDetailsBoffPublicSon.sale_Region_Id:0
          this.salesArea.address = res.data.carFindBOneselfDetailsBoffPublicSon.sale_Region_Name
          this.addRemark = res.data.carFindBOneselfDetailsBoffPublicSon.remark
          this.saleMsg.name = res.data.carFindBOneselfDetailsBoffPublicSon.uname
          this.saleMsg.phone = res.data.carFindBOneselfDetailsBoffPublicSon.phone
          this.productionDate = res.data.carFindBOneselfDetailsBoffPublicSon.produced_Date.split('T')[0]
        }

        if (this.type === '2') {
          setTimeout(() => {
            uni.pageScrollTo({
              scrollTop: 11110,
              duration: 300
            })
          }, 100)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 切换页面状态
    setQuotation() {
      this.type = '2'
      uni.setNavigationBarTitle({
        title: '我要报价'
      })
      setTimeout(() => {
			  uni.pageScrollTo({
			    scrollTop: 11110,
			    duration: 300
			  })
      }, 100)
    },

    //车源所在地
    Listingaddress(type) {
      // type:3---车源所在地 4----销售区域
      uni.navigateTo({
        url: '/pages_findCar/thereGistrationAddress/thereGistrationAddress?type=' + type + '&city=' + this.nowCity
      })
    },
    backTime(it) {
      if (it) {
        let month = it.split('T')[0].split('-')
        let time = month[1] + '-' + month[2] + ' ' + it.split('T')[1]
        return time
      }
    },
    // 报价有效期
    confirmQuotationDate(e) {
      let toast = '报价有效时间不能低于当前时间'
      // ios 不支持yyyy/mm/dd 格式
      // 当前时间
      var myDate = new Date()
      // let year = myDate.getFullYear()
      // let month = myDate.getMonth() + 1
      // let day = myDate.getDate()
      // let today = year + '-' + month + '-' + day
      if (this.quotationDateStatus) {
        this.quotationDate = ''
        this.quotationDateStatus = false
        this.quotationDate = e.year + '-' + e.month + '-' + e.day
        toast = '报价有效时间不能低于当前时间'
        // 当前时间
        if (new Date(this.quotationDate) > myDate) {} else {
          uni.showToast({
            title: toast,
            duration: 2000,
            icon: 'none'
          })
          this.quotationDate = ''
          return
        }
      } else {
        this.productionDate = '', //生产日期
        this.productionDateStatus = false, //生产日期有效期
        this.productionDate = e.year + '-' + e.month + '-' + e.day
        toast = '生产日期不能大于当前时间'
        // 当前时间
        if (new Date(this.productionDate) < myDate) {} else {
          uni.showToast({
            title: toast,
            duration: 2000,
            icon: 'none'
          })
          this.productionDate = ''
          return
        }
      }

    },
    // 手续
    getFormalties(val) {
      this.formalitiesValue = val
      this.formalitiesStatus = false
    },
    // 详情
    selectCarDetail(val) {
      this.carDetail = val
      this.carDetailStatus = false
    },
    gethideshow(phNum, type) {
      let returnText = ''
      if (phNum) {
        if (type === 'phone') {
          returnText = phNum.substr(0, 3) + '****' + phNum.substr(7)
        } else if (type === 'name') {
          returnText = phNum.substr(0, 1) + '**'
        }
        return returnText
      }
    },
    // 获取报价联系人信息
    chooseContact() {
      if (this.saleMsg) {
        uni.navigateTo({
          url: '/pages_common/chooseContact/chooseContact?contactIndex=' + this.saleMsg.index
        })
      } else {
        uni.navigateTo({
          url: '/pages_common/chooseContact/chooseContact'
        })
      }

    },
    // 报价
    async AddCarQuotedPrice() {
      console.log(this.salesArea)
      uni.showLoading({
        title: '报价中'
      })
      let data = {
        'car_find_d_id': this.id,
        'car_quoted_contact_b_id': this.saleMsg.id,
        'store_id': this.$store.state.userInfoDetail.store.storeId,
        'quoted_price': this.myquotation,
        'deposit': this.myDeposit,
        'deadline': this.quotationDate,
        'car_source_region_id': this.carAdress.id,
        'car_source_region_name': this.carAdress.address,
        'service_charge': this.formalitiesValue.value,
        'car_details': this.carDetail.value,
        'sale_region_id': this.salesArea.id?this.salesArea.id:0,
        'sale_region_name': this.salesArea.address,
        'produced_Date': this.productionDate,
        'remark': this.addRemark,
      }
      for (var i in data) {
        if (i !== 'remark') {
          // console.log(i,data[i])
          if (data[i] === ' ' || data[i] === null || data[i] === undefined || data[i] === '') {
            uni.showToast({
              title: '请先完善报价信息！',
              duration: 2000,
              icon: 'none'
            })
            return false
          }
        }
      }
      try {
        const res = await this.$api.findCar.AddCarQuotedPrice(data)
        console.log(res)
        this.success = true
				 uni.hideLoading()
      } catch (err) {
        console.log(err)
				 uni.hideLoading()
      }
    },
    close() {
      this.success = false
      uni.redirectTo({
        url: '/pages_findCar/myOffer/myOffer'
      })
    },
    blurMoney(e) {
      console.log(e)
      if(this.keys===2){
        if ( this.myDeposit < 800) {
          this.myDeposit = ''
          uni.showToast({
            title: '定金金额不能低于800',
            duration: 2000,
            icon: 'none'
          })
        }
      }
    },
    // 获取报价值
    getmyquotation(e) {
      console.log(e)
      if(this.keys===1){
        this.myquotation = this.myquotation + e + ''
      }else{
        this.myDeposit = this.myDeposit + e + ''
      }
    },
    // 退格键被点击
    backspace() {
      // 删除value的最后一个字符
      if(this.keys===1){
        if (this.myquotation.length) this.myquotation = this.myquotation.substr(0, this.myquotation.length - 1)
      }else{
        if (this.myDeposit.length) this.myDeposit = this.myDeposit.substr(0, this.myDeposit.length - 1)
      }
      console.log(this.myquotation)
    }
  }
}
